package com.boot.system.controller;


import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysDeptDTO;
import com.boot.system.wrapper.SysDeptWrapper;
import com.boot.system.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/1/7
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/dept")
@Tag(name = "部门管理")
public class SysDeptController {

    private ISysDeptService sysDeptService;

    @GetMapping("list")
    @Operation(summary = "列表")
    public R list(Map<String, Object> params) {
        return R.ok(sysDeptService.selectList(params));
    }

    /**
     * 新增菜单
     */
    @PostMapping
    @Operation(summary = "新增")
    @Log(title = "部门管理", content = "新增")
    public R insert(@RequestBody SysDeptDTO sysDeptDTO) {
        return R.ok(sysDeptService.save(SysDeptWrapper.build().dtoEntity(sysDeptDTO)));
    }

    /**
     * 修改菜单
     *
     * @param sysDeptDTO
     * @return
     */
    @PutMapping
    @Operation(summary = "修改")
    @Log(title = "部门管理", content = "修改")
    public R update(@RequestBody SysDeptDTO sysDeptDTO) {
        return R.ok(sysDeptService.updateById(SysDeptWrapper.build().dtoEntity(sysDeptDTO)));
    }

    /**
     * 查询菜单
     */
    @GetMapping
    @Operation(summary = "详情")
    public R view(@RequestParam String id) {
        return R.ok(sysDeptService.getById(id));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping
    @Operation(summary = "删除")
    @Log(title = "部门管理", content = "删除")
    public R delete(String ids) {
        return R.ok(sysDeptService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    /**
     * 获取部门树形结构
     */
    @GetMapping("tree")
    @Operation(summary = "树形列表")
    public R getTreeList() {
        return R.ok(sysDeptService.deptTree());
    }
}
